package org.example.service.impl;


import lombok.val;
import org.example.mapper.UserMapper;
import org.example.pojo.Result;
import org.example.pojo.User;
import org.example.service.UserService;
import org.example.utils.JwtUtil;
import org.example.utils.Md5Util;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public User findByUserName(String username) {
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public Result register(String username, String password) {
        User u = userMapper.findByUserName(username);
        if (u == null) {
            String md5 = Md5Util.getMD5String(password);
            userMapper.add(username, md5);
            return Result.success();
        } else {
            return Result.error("用户名已被占用");
        }
    }

    @Override
    public Result<String> login(String username, String password) {

        User loginUser = userMapper.findByUserName(username);
        if (loginUser == null) {
            return Result.error("用户名错误");
        }

        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //登录成功
            Map< String, Object > claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
//            ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();
//            operations.set(token,token,12, TimeUnit.HOURS);
            return Result.success(token);

        }
        return Result.error("密码错误");
    }

    @Override
    public Result<User> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username =(String) map.get("username");
        User user = userMapper.findByUserName(username);
        return Result.success(user);
    }

    @Override
    public Result update(User user) {
        userMapper.update(user);
        User u = userMapper.findById(user.getId());
        return Result.success(u);
    }

    @Override
    public Result updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl, id);
        return Result.success();
    }

    @Override
    public Result updatePwd(String oldPwd, String newPwd, String rePwd,String token) {
        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
            return Result.error("参数错误");
        }
        if(!newPwd.equals(rePwd)){
            return Result.error("密码不一致");
        }
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id =(Integer) map.get("id");
        User u = userMapper.findById(id);
        if(!u.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("原密码错误");
        }
        userMapper.updatePwd(Md5Util.getMD5String(newPwd), id);

//        ValueOperations<String, String> op = stringRedisTemplate.opsForValue();
//        op.getOperations().delete(token);
        return Result.success();
    }
}
